var page = 1;			//滚动ajax 分组获得数据
var flag =true;			//滚动ajax获取数据控制器
var allpage;			//ajax总分组个数
var flag_if =true;            //控制ajax请求
var shop = 0;            //店铺id
var classification_id = 0;  //分类id
var status_value = 0;      //状态
var product_title = 0;
var vm = new Vue({
	el:"#app",
	data:{
        myseach:"",
		shopbox:false,
		shopname:"全部",		//初始化店铺名称
		shoplist:[],			//全部店铺数据
		classifys:[],   //商品类目
		states:[{text:"新品",state_id:0},{text:"正常",state_id:1},{text:"清仓",state_id:2},{text:"淘汰",state_id:3},{text:"赠品",state_id:4}],	//商品状态
		items:[],			//商品列表
		lists:[]			//商品二级类目
	},
	methods:{
        specificationtext:function (n) {
			if(n == 0){
				return '采购规格:';
			}
			if(n == 1){
                return '销售规格:';
			}
        },
        search:function () {
        	var product_title = this.myseach;
            var data ={
                shop_id:shop,
                status:status_value,
				product_title:product_title
            };
            this.$http.post('/products/product_show',data,{emulateJSON: true}).then(function(res){
                this.items = res.data.data;
                allpage = res.data.res;
                navhide();
            },function(){

            });
			console.log(this.myseach);
        },
		shopshow:function(){				//店铺选择页显示
			document.getElementsByTagName('body')[0].style.overflow = 'hidden';
    		document.getElementsByTagName('html')[0].style.overflow = 'hidden';
			this.shopbox = true;
		},
        allclass:function(){
			$.each($('.classify li'),function () {
                $('.classify li').removeClass('choice');
                $('.classify li').eq(0).addClass('choice');
            });
            var data ={
                shop_id:shop,
                status:status_value,
                product_title:product_title
            };
            this.$http.post('/products/product_show',data,{emulateJSON: true}).then(function(res){
                this.items = res.data.data;
                allpage = res.data.res;
                navhide();
            },function(){

            });
		},
		shopchoice:function(index){			//店铺选择确认
			document.getElementsByTagName('body')[0].style.overflow = 'auto';
    		document.getElementsByTagName('html')[0].style.overflow = 'auto';
            shop = this.shoplist[index].shop_id;
            var data ={
                classification_id:classification_id,
                shop_id:shop,
                status:status_value,
                product_title:product_title
            };
            this.$http.post('/products/product_show',data,{emulateJSON: true}).then(function(res){
                this.items = res.data.data;
                this.shopname = this.shoplist[index].shop_name;
                allpage = res.data.res;
                this.shopbox = false;
            },function(){

            });

		},
		togglenav:function(event,n){	//筛选窗口显隐														
			choiceshow(event,n);						
		},
		screen:function(n,index,evnet){		//筛选点击
			choiceclick(n,index,evnet);							
		},
		mychioescreen:function(index,event){		//二级类目点击
			$.each($('.child li'), function() {
					$(this).removeClass('choice');
				});
			$(event.target).addClass('choice');
            classification_id  = this.lists[index].id;
            var data ={
                classification_id:classification_id,
				shop_id:shop,
				status:status_value,
                product_title:product_title
			};
			this.$http.post('/products/product_show',data,{emulateJSON: true}).then(function(res){
                this.items = res.data.data;
                allpage = res.data.res;
				navhide();
			},function(){
				
			});											
		},
		sorttype:function(n,event){
			if(n == 0){		//库存排序
				this.sort(n,event);							
			};
			if(n == 1){		//销售额排序	
				this.sort(n,event);
			};
			if(n == 2){		//销售数量排序
				this.sort(n,event);
			};
		},
		sort:function(n,event){	//排序
			$.each($('.nav li'), function(index) {								
				if($(this)[0] == $(event.target)[0]){
						$('.nav li').removeClass('active');
						$(event.target).addClass('active');
						$(event.target).find('i').eq(1).hide();
						$(event.target).find('i').eq(0).css('top','12px');
							if($(event.target).find('i').eq(0).hasClass('fa-caret-up')){		//从低到高
								$(event.target).find('i').eq(0).removeClass('fa-caret-up');
								$(event.target).find('i').eq(0).addClass('fa-caret-down');											
								if(n == 0){
									vm.items.sort(vm.down("stocks"));
								}
								if(n == 1){
									vm.items.sort(vm.down("last_price"));
								}
								if(n == 2){
									vm.items.sort(vm.down("weekly_sales"));
								}
							}else{																//从高到低
								$(event.target).find('i').eq(0).addClass('fa-caret-up');		
								$(event.target).find('i').eq(0).removeClass('fa-caret-down');	
								if(n == 0){
									vm.items.sort(vm.up("stocks"));
								}
								if(n == 1){
									vm.items.sort(vm.up("last_price"));
								}
								if(n == 2){
									vm.items.sort(vm.up("weekly_sales"));
								}																						
							};
				}else{						
					$(this).find('i').show();
					$(this).find('i').eq(0).css('top','9px').attr('class','fa fa-caret-up');
					$(this).find('i').eq(1).css('bottom','9px').attr('class','fa fa-caret-down');
				}							
			});					
		},
		up:function(prop){				//从高到低排序函数
		    return function (obj1, obj2) {
		        var val1 = obj1[prop];
		        var val2 = obj2[prop];
		        if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
		            val1 = Number(val1);
		            val2 = Number(val2);
		        }
		        if (val1 < val2) {
		            return -1;
		        } else if (val1 > val2) {
		            return 1;
		        } else {
		            return 0;
		        }            
		    }				
		},
		down:function(prop){				//从低到高排序函数
			    return function (obj1, obj2) {
		        var val1 = obj1[prop];
		        var val2 = obj2[prop];
		        if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
		            val1 = Number(val1);
		            val2 = Number(val2);
		        }
		        if (val1 > val2) {
		            return -1;
		        } else if (val1 < val2) {
		            return 1;
		        } else {
		            return 0;
		        }            
		    } 	
		},
		policyup:function(event){		//政策收起	
			$(event.target).parents('.policy').slideUp(200);
		},
		policydown:function(event){		//政策展开
			$(event.target).parents('.info').next().slideDown(200);
		},
		change:function(event,listindex,index){			//商品规格切换
			$(event.target).parents('.standard').find('span').removeClass('change');
			$(event.target).addClass('change');
			this.items[listindex].last_price = this.items[listindex].specification_arr[index].last_price;
			this.items[listindex].price = this.items[listindex].specification_arr[index].price;
		},
        change_price:function(id){
			window.location.href = "/index/change_price?sku_id="+id;
		},
		scroll:function(){				//滚东请求ajax
			$(window).scroll(function(){
				var remainHeight = document.documentElement.scrollHeight - $(window).scrollTop();
				if(remainHeight - $(window).height()<20){
					if(flag){
						if(flag_if ){
                            flag_if = false;
                            var data ={
                                shop_id:shop,
                                status:status_value,
                                page:page,
                                classification_id:classification_id,
								product_title:product_title
                            };
                            this.$http.post('/products/product_show',data,{emulateJSON: true}).then(function(res){
                                for(var i in res.data.data){
                                    this.items.push(res.data.data[i]);
                                }
                                if(page == allpage){
                                    flag = false;
                                }
                                page++;
                                console.log(page);
                                console.log(flag_if);
                                flag_if = true;
                            },function(){

                            });
						}

					}
				};
			}.bind(this));
		}
	},
	created:function(){
        //var stocks = window.location.search.split("?")[1].split("=")[1];//取地址参数部分
		this.$http.get('/products/product_show',{emulateJSON: true}).then(function(res){			//初始化数据
			this.shoplist = res.data.date;	//店铺列表
			this.items = res.data.data; //商品列表
			console.log(this.items);
			this.classifys = res.data.arr; //分类类目
            allpage = res.data.res //总分组的值
			console.log(res.data.res);
		},function(){
			
		});
	},
	mounted:function(){							
			this.scroll();
		}
});

function choiceshow(event,n){					//分类，状态导航点击
		$.each($(".nav li"), function() {
			$(".nav li").removeClass('active');
		});
		if(n == 0){								//分类点击
					$(".nav li").eq(1).find('i').attr('class','fa fa-caret-up');
					$('.state').hide();
					if($('.classify').is(':hidden')){
						$('.classify').slideDown(200);
						$('.box').show();
						$(event.target).addClass('active');
						$(event.target).find('i').attr('class','fa fa-caret-down');
					}else{
						$('.classify').slideUp(200);
						$('.box').hide();
						$(event.target).removeClass('active');
						$(event.target).find('i').attr('class','fa fa-caret-up');
					}
				}
				if(n == 1){							//状态点击	
					$(".nav li").eq(0).find('i').attr('class','fa fa-caret-up');
					$('.classify').hide();
					if($('.state').is(':hidden')){
						$('.state').slideDown(200);
						$('.box').show();
						$(event.target).addClass('active');
						$(event.target).find('i').attr('class','fa fa-caret-down');
					}else{
						$('.state').slideUp(200);
						$('.box').hide();
						$(event.target).removeClass('active');
						$(event.target).find('i').attr('class','fa fa-caret-up');
					}
				}
		}
function choiceclick(n,index,evnet){		//分类，状态目录点击
			if(n == 0){					//分类
				$.each($('.classify li'), function() {
					$(this).removeClass('choice');
				});
				$(event.target).addClass('choice');
				vm.lists = vm.classifys[index].lists;
				$('.child').slideDown(200);
				return false;
			}
			if(n == 1){					//状态
				$.each($('.state li'), function() {
					$(this).removeClass('choice');
				});
				$(event.target).addClass('choice');
                status_value = vm.states[index].state_id
                var data ={
                    shop_id:shop,
                    status:status_value,
                    classification_id:classification_id,
                    product_title:product_title
                };
				vm.$http.post('/products/product_show',data,{emulateJSON: true}).then(function(res){
						vm.items = res.data.data;
                    allpage = res.data.res;
				},function(){
						
					});
			}
			navhide();
	
};
function nofind(){  //图片没找到。显示默认图片
	var img = event.srcElement;
	img.src = "http://weixin.ismbao.com/tb/140x140/tb/140x140/upload/201610/22/1477129969570610.jpg";
	img.onerror=null; //控制不要一直跳动
} ;	
function navhide(){		//隐藏分类，状态目录
	$(".nav li").eq(0).find('i').attr('class','fa fa-caret-up');
	$(".nav li").eq(1).find('i').attr('class','fa fa-caret-up');
	$('.state').hide();
	$('.classify').hide();
	$('.box').hide();
}
function all_shop(){
	vm.shopname = '全部';
	window.location.reload();
}